package com.hy.mydesktop.client.mvc.desktop;

import java.util.Map;

import com.extjs.gxt.ui.client.mvc.AppEvent;
import com.extjs.gxt.ui.client.widget.menu.MenuItem;
import com.hy.mydesktop.client.component.event.AppEvents;
import com.hy.mydesktop.client.component.factory.GxtDesktopComponentFactory;
import com.hy.mydesktop.client.component.mvc.event.EventStructureMetaModel;
import com.hy.mydesktop.client.mvc.base.BaseController;
import com.hy.mydesktop.client.mvc.util.ControllerUtil;
import com.hy.mydesktop.client.mvc.window.WindowDisplay;

/**
 * 
 * <ul>
 * <li>开发作者：花宏宇</li>
 * <li>设计日期：2010-9-15；时间：上午09:30:18</li>
 * <li>类型名称：MenuItemController</li>
 * <li>设计目的：</li>
 * </ul>
 * <ul>
 * <b>修订编号：</b>
 * <li>修订日期：</li>
 * <li>修订作者：</li>
 * <li>修订原因：</li>
 * <li>修订内容：</li>
 * </ul>
 */
public class DesktopMenuItemController extends BaseController {

	private DesktopMenuItemPresenter desktopMenuItemPresenter;
	
	public DesktopMenuItemController() {
		
		super();
		super.printControllerForInit(getClass());
	    
	}

	@Override
	public void handleEvent(AppEvent event) {
		EventStructureMetaModel eventStructureMetaModel =(EventStructureMetaModel)((Map<String, Object>)event.getData()).get("eventstructuremetamodel");
		//0002
		if (AppEvents.DESKTOP_INIT_FOR_LOGGED_IN_USER==event.getType()) {
			//通过EventStructureMetaModel识别，是否应该处理该事件
			if (ControllerUtil.isHandle(eventStructureMetaModel, this)) {
//if (eventStructureMetaModel==null||this.getId()==null||eventStructureMetaModel.getControllerId()==null||eventStructureMetaModel.getControllerId().equals(this.getId())) {
				forwardToView(desktopMenuItemPresenter, event);
				 this.forwardToChild(event);
			}
			 
		}; 
		//0003
		if (AppEvents.DESKTOP_SHORTCUT_ONCLICK_INIT==event.getType()) {
			//通过EventStructureMetaModel识别，是否应该处理该事件
			if (ControllerUtil.isHandle(eventStructureMetaModel, this)) {
//if (eventStructureMetaModel==null||this.getId()==null||eventStructureMetaModel.getControllerId()==null||eventStructureMetaModel.getControllerId().equals(this.getId())) {
				forwardToView(desktopMenuItemPresenter, event);
				 this.forwardToChild(event);
			}
		};
		//0004
		if (AppEvents.DESKTOP_SHORTCUT_ONCLICK==event.getType()) {
			//通过EventStructureMetaModel识别，是否应该处理该事件
			if (ControllerUtil.isHandle(eventStructureMetaModel, this)) {
//if (eventStructureMetaModel==null||this.getId()==null||eventStructureMetaModel.getControllerId()==null||eventStructureMetaModel.getControllerId().equals(this.getId())) {
				forwardToView(desktopMenuItemPresenter, event);
				 this.forwardToChild(event);
			}
		};
		//00005
		if (AppEvents.DESKTOP_MENUITEM_ONCLICK_INIT==event.getType()) {
			//通过EventStructureMetaModel识别，是否应该处理该事件
			if (ControllerUtil.isHandle(eventStructureMetaModel, this)) {
//if (eventStructureMetaModel==null||this.getId()==null||eventStructureMetaModel.getControllerId()==null||eventStructureMetaModel.getControllerId().equals(this.getId())) {
				forwardToView(desktopMenuItemPresenter, event);
				 this.forwardToChild(event);
			}
		};
		/**
		 * 将事件转发给子节点，及子控制器
		 * 注:当前控制器，含有子节点的时候，必不可少
		 */
		//this.forwardToChild(event);
		/*枚举每一个可能需要响应的事件，以及其需要的处理方式
		ifelse switch (key) {
			case value:
				
				break;

			default:
				break;
		}*/
	}
	
	public void initialize() {
		

		MenuItem menuItem = (MenuItem)GxtDesktopComponentFactory.addOneGxtComponentToDesktop( getGxtComponentMetaNodeModel());
		DesktopMenuItemDisplay display = new DesktopMenuItemDisplay(menuItem);
		desktopMenuItemPresenter = new DesktopMenuItemPresenter(this,display);

		 
	  }
	
	protected void onError(AppEvent ae) {
	    System.out.println("error: " + ae.<Object>getData());
	  }
}
